package ldh.im.rpc.dto;

public class RpcData<T> {

    private String path;                            // rpc路径
    private Integer version;                        // 版本号
    private Integer status;                         // 请求状态
    private Integer authCode;                       // 认证code
    private RpcExtData extData = new RpcExtData();  // 扩展信息
    private T data;                                 // 交互数据

    public RpcData() {}

    public static RpcData buildSuccessRpcData(String path, String localSeql, Object resultObj) {
        RpcData resultRpcData = new RpcData();
        resultRpcData.setStatus(100);  // 成功
        resultRpcData.setPath(path);
        resultRpcData.setVersion(1);
        resultRpcData.setAuthCode(0);
        RpcExtData rpcExtData = new RpcExtData();
        rpcExtData.getExtDataMap().put(RpcExtData.LOCAL_SEQ, localSeql);
        resultRpcData.setExtData(rpcExtData);
        resultRpcData.setData(resultObj);
        return resultRpcData;
    }

    public static RpcData buildErrorRpcData(String path, String localSeql, Object resultObj) {
        RpcData resultRpcData = buildSuccessRpcData(path, localSeql, resultObj);
        resultRpcData.setStatus(-1);
        return resultRpcData;
    }

    public RpcData(T dto) {
        this.data = dto;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public Integer getAuthCode() {
        return authCode;
    }

    public void setAuthCode(Integer authCode) {
        this.authCode = authCode;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
        if (path == null || path.contains(" ")) {
            throw new IllegalArgumentException("路径不正确");
        }
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public RpcExtData getExtData() {
        return extData;
    }

    public void setExtData(RpcExtData extData) {
        this.extData = extData;
    }

    public void setLocalSeq(String localSeq) {
        RpcExtData rpcExtData = new RpcExtData();
        rpcExtData.getExtDataMap().put(RpcExtData.LOCAL_SEQ, localSeq);
        this.setExtData(rpcExtData);
    }
}
